// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// a [author] rhyme_lph
// z [email]  rhymelph@gmail.com
// q [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/batch_invenotries_entity.dart';
import 'package:finance_app/src/entities/inventory_entity.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/provider/dashboard/products_services/inventory_adjustment_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/adjust_number_form.dart';
import 'package:finance_app/src/widgets/form/attach_file_form.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/text_area_input_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:tuple/tuple.dart';

export 'package:finance_app/src/entities/inventory_entity.dart';

@RouterMeta(paramName: "inventoryAdjustmentPage")
class InventoryAdjustmentPage extends StatefulWidget {
  final InventoryEntity data;
  final String batchId;
  final int id;

  const InventoryAdjustmentPage({Key key, this.data, this.batchId, this.id})
      : super(key: key);

  @override
  _InventoryAdjustmentPageState createState() =>
      _InventoryAdjustmentPageState();
}

class _InventoryAdjustmentPageState extends State<InventoryAdjustmentPage>
    with PagePresenter<InventoryAdjustmentPage, InventoryAdjustmentProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.adjust_inventory,
      );

  @override
  void initState() {
    if (widget.id == null) {
      provider.initData(widget.batchId, widget.data);
    } else {
      provider.id = widget.id;
    }
    super.initState();
  }

  @override
  Widget get buildWidget => FormEditStatusInheritedWidget(
        isEdit: false,
        child: ListView(
          children: [
            // NormalFormWrapper(
            //   child: Row(
            //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //     children: <String>[
            //       'Return Item',
            //       'Stock Adjustment',
            //     ]
            //         .map((e) => listBuilder<bool>(
            //               selector: (_, __) => __.currentSelect == e,
            //               builder: (BuildContext context, bool value,
            //                       Widget child) =>
            //                   PressedOpacityButton(
            //                 onPressed: () => provider.onChangeSelect(e),
            //                 child: NormalRadio(
            //                   title: LanguageUtils.translateAction(e),
            //                   isSelect: value,
            //                   isEnable: false,
            //                 ),
            //               ),
            //             ))
            //         .toList(),
            //   ),
            // ),
            // AppSpaces.grayH05,
            listBuilder<String>(
              selector: (_, __) => DateTimeUtils.formatDateTimeString(
                  widget.data?.createdAt ?? __.data?.createdAt),
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.adjustment_date,
                value: value,
                onTap: provider.onChnageAdjustMentDate,
              ),
            ),
            listBuilder<int>(
              selector: (_, __) => provider.batchInventories?.length,
              builder: (BuildContext context, int value, Widget child) =>
                  value != null
                      ? Column(
                          children: List.generate(
                              value,
                              (index) => listBuilder<BatchInvenotriesEntity>(
                                    selector: (_, __) =>
                                        provider.batchInventories[index],
                                    builder: (BuildContext context,
                                            BatchInvenotriesEntity value,
                                            Widget child) =>
                                        AdjustNumberForm(
                                      title: value.entityName ?? value.name,
                                      numberTitle: S.current.actual,
                                      content: S.current.stock_(
                                          value.inventoryLeft - value.quantity),
                                      initialNumber: value.inventoryLeft,
                                      otherValue: value.quantity,
                                    ),
                                  )),
                        )
                      : Container(),
            ),
            AppSpaces.grayH05,
            TextAreaInputForm(
              title: S.current.additional_notes_optional,
              controller: provider.additionalController,
              focusNode: provider.additionalFocusNode,
            ),
            NormalFormWrapper(
              child:
                  listBuilder<Tuple2<List<String>, List<OcrAttachmentEntity>>>(
                selector: (_, __) => Tuple2(
                    List.from(__.files), List.from(__.attachments ?? [])),
                builder: (BuildContext context,
                    Tuple2<List<String>, List<OcrAttachmentEntity>> value,
                    Widget child) {
                  return AttachFileForm(
                    onAdd: provider.onAddAttachment,
                    files: value.item1,
                    onSelectFile: provider.onSelectFile,
                    attachments: value.item2,
                    onSelectAttachment: provider.onSelectAttachment,
                  );
                },
              ),
            ),
          ],
        ),
      );
}
